﻿using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Model;

namespace Backend.JYD.EFDAL.DAL
{
	public class RoleInfoDAL : Backend.JYD.IDAL.IRoleInfo
	{
		#region 查询数据RoleInfo

		/// <summary>
		/// 查询数据
		/// </summary>
		public PageInfo<RoleInfo> GetRoleInfoByPage(PageInfo page)
		{
			using (JydDataContext db = new JydDataContext())
			{
                IQueryable<RoleInfo> query = db.RoleInfoes.Include("RoleAuthInfoes").Where(p => p.Status > 0);
				//其他条件代码
				return new PageInfo<RoleInfo>(page, query);
			}
		}

        public IList<RoleInfo> GetAllRoleInfo()
        {
            using (JydDataContext db = new JydDataContext())
            {
                return db.RoleInfoes.Include("RoleAuthInfoes").Where(p => p.Status > 0).ToList();
            }
        }

		/// <summary>
		/// 查询数据
		/// </summary>
		public RoleInfo GetRoleInfoByRoleId(Int64 roleId)
		{
			using (JydDataContext db = new JydDataContext())
			{
                IQueryable<RoleInfo> query = db.RoleInfoes.Include("RoleAuthInfoes").Where(p => p.RoleId == roleId);
				return query.FirstOrDefault();
			}
		}
        public IList<RoleInfo> GetEmpRoleInfoByEmpId(Int64 empId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                IQueryable<RoleInfo> query = from p in db.EmpRoleInfoes.Include("RoleInfo.RoleAuthInfoes")
                        where p.Status > 0 && p.RoleInfo.Status > 0
                        select p.RoleInfo;

                //其他条件代码
                return query == null ? null : query.ToList();
            }
        }

		#endregion

		#region 插入数据

		/// <summary>
		/// 插入数据RoleInfo
		/// </summary>
		public Int64 AddRoleInfo(RoleInfo info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				db.RoleInfoes.Add(info);
				db.SaveChanges();
				return info.RoleId;
			}
		}
		#endregion

		#region 修改数据RoleInfo

		/// <summary>
		/// 根据字段（RoleId）修改数据RoleInfo
		/// </summary>
		public bool UpdateRoleInfo(RoleInfo info)
		{
			using (JydDataContext db = new JydDataContext())
			{
				var query = db.RoleInfoes.Where(p => p.RoleId == info.RoleId);
				if (query != null)
				{
					var temp = query.FirstOrDefault();
					if (temp != null)
					{
						temp.OpId = info.OpId;
						temp.UpdateTime = DateTime.Now;
						//.......
						return db.SaveChanges() > 0;
					}
				}
				return false;
			}
		}
		#endregion


	}
}